<?php

// +------------------------------------------------------------------------+
// |          LNMP V2.0 for Ubuntu Linux Server, Written by Licess          |
// +------------------------------------------------------------------------+
// |           For more information please visit https://lnmp.org           |
// +------------------------------------------------------------------------+
// |    lnmp status manage: lnmp {start|stop|reload|restart|kill|status}    |
// +------------------------------------------------------------------------+
// |  phpMyAdmin: http://192.168.8.130/phpmyadmin/                                     |
// |  phpinfo: http://192.168.8.130/phpinfo.php                                        |
// |  Prober:  http://192.168.8.130/p.php                                              |
// +------------------------------------------------------------------------+
// |  Add VirtualHost: lnmp vhost add                                       |
// +------------------------------------------------------------------------+
// |  Default directory: /home/wwwroot/default                              |
// +------------------------------------------------------------------------+
// |  MySQL/MariaDB root password: msc6SLRHIxo./Lc]                          |
// +------------------------------------------------------------------------+

// 切换到www：
// su www -s /bin/bash
// chmod 777 azfzjkp_dev/

// cp -p 6R2ym2rPqs.txt ../6R2ym2rPqs.txt
// http://192.168.8.130/azfzjkp_dev/6R2ym2rPqs.txt
// https://jiankangpao.sichuancancer.org/6R2ym2rPqs.txt

// 肿瘤防治健康跑管理员后台：
// 账号：admin  密码：azfzjkp-a
// http://192.168.8.130/azfzjkp_dev/home/login/login.html
// https://jiankangpao.sichuancancer.org/azfzjkp/home/login/login.html

// https://jiankangpao.sichuancancer.org/phpinfo.php

// 肿瘤防治健康跑
// https://jiankangpao.sichuancancer.org/azfzjkp_dev/webhooks.php
// git webhook 自动部署脚本
// 项目存放物理路径,第一次clone时,必须保证该目录为空
// sudo chown -R www:www *

// 1. cd /home/wwwroot/default/azfzjkp_dev/
// 2. lnmp start
// 3. cp -p /home/wwwroot/default/azfzjkp_dev/nginx.conf /usr/local/nginx/conf/nginx.conf && lnmp stop && lnmp start 

// 4.启动redis
// cd /usr/local/redis/bin && ./redis-server &
// vi /usr/local/php/etc/php.ini

// cp -p /usr/local/php/etc/php.ini /home/wwwroot/default/azfzjkp_dev/php.ini 
// cp -p /home/wwwroot/default/azfzjkp_dev/php.ini  /usr/local/php/etc/php.ini 

// AppID(小程序ID)
// wx65e037fa4769e325
// AppSecret(小程序密钥)
// 8501716cff7129294bc478070930067d

// 数据库: root msc6SLRHIxo./Lc] azfzjkp@2024
// http://192.168.8.130/phpmyadmin/
// https://jiankangpao.sichuancancer.org/phpmyadmin/
// cp -p /home/wwwroot/default/azfzjkp_dev/nginx.conf /usr/local/nginx/conf/nginx.conf
// nginx -s reload

$savePath = "/home/wwwroot/default/azfzjkp_dev/";
$gitPath = "gitee.com/isc0362/azfzjkp.git"; //代码仓库
$email = "236595218@qq.com"; //用户仓库邮箱
$name = "236595218@qq.com"; //仓库用户名,一般和邮箱一致即可 iscAT0362

$isClone = true; //设置是否已经Clone到本地,true:已经clone,直接pull,false:先clone.

//如果已经clone过,则直接拉去代码
if ($isClone) {
	$requestBody = file_get_contents("php://input");
	if (empty($requestBody)) {
		die('send fail');
	}

	//解析Git服务器通知过来的JSON信息
	$content = json_decode($requestBody, true);

	//若是主分支且提交数大于0
	if ($content['ref'] == 'refs/heads/develop') {
		$savePath = "/home/wwwroot/default/azfzjkp_dev/";
		// file_put_contents("git-webhook_log.txt", $res_log, FILE_APPEND);//写入日志到log文件中
	} else {
		$savePath = "/home/wwwroot/default/azfzjkp/";
		// file_put_contents("git-webhook_log.txt", $content['ref'], FILE_APPEND);//写入日志到log文件中
	}

	if ($content['total_commits_count'] > 0) {
		$res = PHP_EOL . "pull start --------" . PHP_EOL;
		$cmd = "cd {$savePath} && git pull 2>&1";
		$res .= shell_exec($cmd); //拉去代码,查看phpinfo，里面disable_functions处shell_exec这个是否被禁用

		$res_log = '-------------------------' . $cmd . PHP_EOL;
		$res_log .= $content['user_name'] . ' 在' . date('Y-m-d H:i:s') . '向' . $content['repository']['name'] . '项目的' . $content['ref'] . '分支push了' . $content['total_commits_count'] . '个commit: ';
		$res_log .= $res . PHP_EOL;
		$res_log .= "pull end --------" . PHP_EOL;
		$res_log .= "pull cmd：{$cmd}" . PHP_EOL;
		echo $res_log;
	}
} else {
	$res = "clone start --------" . PHP_EOL;
	//注:在这里需要设置用户邮箱和用户名,不然后面无法拉去代码
	$res .= shell_exec("git config --global user.email {$email}}") . PHP_EOL;
	$res .= shell_exec("git config --global user.name {$name}}") . PHP_EOL;
	$res .= shell_exec("git clone {$gitPath} {$savePath}") . PHP_EOL;
	$res .= "clone end --------" . PHP_EOL;
	// file_put_contents("git-webhook_log.txt", $res, FILE_APPEND);//写入日志到log文件中
}
